package edu.gdkm.demo10;


import java.util.Random;

public class GetGiftThread implements Runnable{
    private Integer gift = 100;
    @Override
    public void run() {
        while (true){
            this.apply();
            if(gift<=0){
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized void apply(){
        if(gift>0){
            int applygift = new Random().nextInt(5)+1 ; // 1-5之间的随机整数
            if(gift<applygift){
                applygift = gift;
            }
            gift = gift -applygift;
            System.out.println("恭喜你成功获得"+applygift+"份赠品,当前赠品还有"+gift+"份");
        }

    }
}
